import { defineStore } from "pinia";
import { ref, computed } from "vue";

export const useUserStore = defineStore("user", () => {
  // 状态
  const playerName = ref(localStorage.getItem("playerName") || "");
  const playerId = ref("");
  const isDrawer = ref(false);

  // 计算属性
  const displayName = computed(() => {
    return playerName.value || `玩家${Math.floor(Math.random() * 1000)}`;
  });

  // 方法
  const setPlayerName = (name: string) => {
    playerName.value = name;
    localStorage.setItem("playerName", name);
  };

  const setPlayerId = (id: string) => {
    playerId.value = id;
  };

  const setIsDrawer = (drawer: boolean) => {
    isDrawer.value = drawer;
  };

  return {
    playerName,
    playerId,
    isDrawer,
    displayName,
    setPlayerName,
    setPlayerId,
    setIsDrawer,
  };
});
